<?php

namespace Prototype;


class Client
{
    /**
     * clone的对象与原对象无关,赋值的对象与原对象关联
     */
    public static function main()
    {
        $product = new Product();
        $product_clone = clone $product;
        $product_assign = $product;
        var_dump(compact('product', 'product_clone', 'product_assign'));

        $product->widget->id = -1;
        var_dump(compact('product', 'product_clone', 'product_assign'));
    }
}


interface Cloneable
{
    public function __clone();
}

class Product implements Cloneable
{

    public Widget $widget;

    public function __construct()
    {
        $this->widget = new Widget();
    }

    public function __clone()
    {
        $this->widget = clone $this->widget;
    }
}

class Widget
{
    public int $id;

    public function __construct()
    {
        $this->id = mt_rand(0, PHP_INT_MAX);
    }
}


Client::main();
